<?php

require_once  __DIR__ . '/includes/authenticate.php';
include_once './db_bb_connect.php';


?>

<div id="viewtrainingspresencecontent">
<h4>View FT Stats by Week</h4>


<form>
	
<select class="form-control"  name="season" id="select4">
		
<?php

if (isset($_POST['season'])) {

	$setseason = $_POST['season'];

echo  "<option selected=\"selected\" value=\"$setseason\"> $setseason is selected</option>";
	
} else {
	echo  "<option selected=\"selected\" disabled=\"disabled\"> Select season</option>";
}


$sql = "SELECT DISTINCT season 
FROM trainingpresence";

$result = $db->query($sql);

while ($row = $result->fetch_assoc()) {
	$season = $row['season'];

	echo "<option value=\"$season\"> $season </option>";
}

?>
</select>

<br>
		
<?php

if (isset($_POST['season'])) {

	?>

<select class="form-control"  name="week" id="select5">

<?php 

if (isset($_POST['week'])) { 

	$setweek = $_POST['week'];

	echo "<option selected=\"selected\" value=\"$setweek\"> Week $setweek is selected</option>";

} else {

	echo "<option selected=\"selected\" disabled=\"disabled\"> Select Week of Training you want to show</option>";

	}



$sql2 = "SELECT DISTINCT week(date,3) as avweeks, year(date) as avyear
FROM trainingft 
WHERE season = '$setseason'
ORDER BY year(date) DESC, week(date,3) DESC";



$result2 = $db->query($sql2);

while ($row2 = $result2->fetch_assoc()) {
	$avweek = $row2['avweeks'];

	echo "<option value=\"$avweek\"> Week $avweek </option>";
}

}

?>
</select>

</form>

<br><br>

<?php

if (isset($_POST['week']) && isset($_POST['season'])) {

    $setweek = $_POST['week'];
    $setseason = $_POST['season'];

?>


 <div class="table-responsive"> 
 <h4>FT stats of season <?= $setseason; ?> week <?= $setweek; ?></h4>      
  <table class="table table-hover">
    <thead>
      <tr>
        <th collspan="2">Name</th>
        <th>Scored / Total</th>
        </tr>
    </thead>
    <tbody>
      



<?php
	
	$sql3 = "SELECT teams.playerID, users.firstname, users.lastname
	FROM teams
	JOIN users 
	ON teams.playerID = users.user_key
	WHERE season = '$setseason' 
	ORDER BY users.playernumber
	";

	if ($result3 = $db->query($sql3)) {

	} else {
		echo "<br><center>Error: " . $sql3 . "<br>" . $db->error . "</center>";
		}

	$result3 = $db->query($sql3);

	while ($row3 = $result3->fetch_assoc()) {

		$playerID = $row3['playerID'];
		$firstname = $row3['firstname'];
		$lastname = $row3['lastname'];
		

		$sql6 = "SELECT SUM(ftscored) AS totalpresence, 
		SUM(fttotal) AS totaltraining 
		FROM trainingft
		WHERE playerID = '$playerID' 
		AND season = '$setseason' 
		AND WEEK(date,3) = '$setweek'";

		if ($result6 = $db->query($sql6)){

		} else {
		echo "<br><center>Error: " . $sql3 . "<br>" . $db->error . "</center>";
		}

		$result6 = $db->query($sql6);
		$row6 = $result6->fetch_assoc();

		$totalpresence = $row6['totalpresence'];
		$totaltraining = $row6['totaltraining'];


?>
	 <tr>
        <td><?= $firstname ?> <?= $lastname ?> </td>
        <td><?= $totalpresence ?> / <?= $totaltraining ?> </td>

      </tr>

<?php
}

}

?>

    </tbody>
  </table>
  </div>

</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<script type="text/javascript">
	
$("#select4").on("change", function(e) {
    postData = $(this.form).serialize();
    $.ajax({
        type: "POST",
        url: "./training_viewftstatsweek.php",
        data: postData,
        success: function(data) { 
        	//alert(postData);
            $("#viewtrainingspresencecontent").html(data); 
        },
        error: function (xhr, status, error) {
        	$("#viewtrainingspresencecontent").html(xhr.responseText); 
        }
      }); 
    e.preventDefault();
});  

$("#select5").on("change", function(e) {
    postData = $(this.form).serialize();
    $.ajax({
        type: "POST",
        url: "./training_viewftstatsweek.php",
        data: postData,
        success: function(data) { 
        	//alert(postData);
            $("#viewtrainingspresencecontent").html(data); 
        },
        error: function (xhr, status, error) {
        	$("#viewtrainingspresencecontent").html(xhr.responseText); 
        }
      }); 
    e.preventDefault();
});  

</script>